package com.huanbao.oil.app.application;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.maning.librarycrashmonitor.MCrashMonitor;
import com.maning.librarycrashmonitor.listener.MCrashCallBack;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.xuhao.didi.socket.client.sdk.OkSocket;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

public class BaseApplication extends Application {
    public static String msg = "";
    public static final String TAG = "tag";

    public static int pids[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22,
            26,27,28,29,30,36,31,32,33,34,35,36,37,46,47,48,49,
            50,51,52,53,54,55,56,57,
            101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
            1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008,
             2001,2000,3000};
    public static int pid_ids[] = {5, 5, 6,  30, 31,
            1, 1, 2, 2,
            5, 6, 7,4,
            3,4,5,3};
    public static String pid_names[] = {"high_level_value", "low_level_value","high_level_value", "work_time", "free_time",
            "high_level_value", "low_level_value", "high_level_value", "low_level_value",
            "base_vaule", "base_vaule", "base_vaule","high_level_value",
            "base_vaule","base_vaule", "higher_level_value", "low_level_value"

    };


    public static BaseApplication mApp = null;
    public List<Activity> mActivitys = new ArrayList();

    public static int screenWidth = 0;
    public long exitTime = 0;
    public Context context_aplication;

    public static BaseApplication getSelf() {
        return mApp;
    }


    @SuppressWarnings("deprecation")
    @Override
    public void onCreate() {
        super.onCreate();
        mApp = (BaseApplication) getApplicationContext();
        initError();

        MCrashMonitor.init(this, false, new MCrashCallBack() {
            @Override
            public void onCrash(File file) {
                //可以在这里保存标识，下次再次进入把日志发送给服务器
//                Log.i(TAG, "CrashMonitor回调:" + file.getAbsolutePath());
            }
        });


//        CrashHandler.getInstance().init(this);

//        Logger.addLogAdapter(new AndroidLogAdapter());
//
//        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor(TAG))
//                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
//                .readTimeout(10000L, TimeUnit.MILLISECONDS)
//                //其他配置
//                .build();
//
//        OkHttpUtils.initClient(okHttpClient);
//
//
//        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
////log打印级别，决定了log显示的详细程度
//        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
////log颜色级别，决定了log在控制台显示的颜色
//        loggingInterceptor.setColorLevel(Level.ALL);
////        builder.addInterceptor(new LogInterceptor());
////第三方的开源库，使用通知显示当前请求的log
////        builder.addInterceptor(new ChuckInterceptor(this));
////全局的读取超时时间
//        builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
////全局的写入超时时间
//        builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
////全局的连接超时时间
//        builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
//
//        OkGo.getInstance().init(this)                       //必须调用初始化
//                .setOkHttpClient(builder.build())               //必须设置OkHttpClient
//                .setCacheMode(CacheMode.NO_CACHE)               //全局统一缓存模式，默认不使用缓存，可以不传
//                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)   //全局统一缓存时间，默认永不过期，可以不传
//                .setRetryCount(3);                            //全局统一超时重连次数，默认为三次，那么最差的情况会请求4次
//        // (一次原始请求，三次重连请求)，不需要可以设置为0


    }


    public void pull(Activity paramActivity) {
        for (int i = 0; i < mActivitys.size(); i++) {
            if (this.mActivitys.get(i) == paramActivity) {
                this.mActivitys.remove(i);
                return;
            }
        }
    }

    public void push(Activity paramActivity) {
        for (int i = 0; i < mActivitys.size(); i++) {
            if (this.mActivitys.get(i) == paramActivity) {
                this.mActivitys.add(paramActivity);
                return;
            }
        }
    }

    public void exit(Context context) {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            exitTime = System.currentTimeMillis();
        } else {
            for (Activity activity : mActivitys) {
                activity.finish();
            }
            // 退回到桌面，假退出
            // Intent i = new Intent(Intent.ACTION_MAIN);
            // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // i.addCategory(Intent.CATEGORY_HOME);
            // context.startActivity(i);
        }
    }

    @SuppressLint("UseSparseArrays")
    private HashMap<Integer, String> errorMap = new HashMap<Integer, String>();

    private void initError() {
        errorMap.put(1, "查询成功");
        errorMap.put(-1, "未登录");
        errorMap.put(-2, "参数不全");
        errorMap.put(-3, "手机号或密码不正确");
        errorMap.put(-4, "短信验证码错误");
        errorMap.put(-5, "手机号码已存在");
    }

    /**
     * 根据status获取错误信息
     *
     * @param status
     * @return
     */
    public String getError(int status) {
        if (errorMap.containsKey(status))
            return errorMap.get(status);
        return "";
    }

}
